import { _decorator, Component, Node, SpringJoint2D, Sprite, UITransform,Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import EventManager from '../../Runtime/EventManger';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM } from '../../Enums';

import { EntityManager } from '../../Base/EntityManager';
import DataManager from '../../Runtime/DataManager';
import { DoorStateMachine } from './DoorStateMachine';
import { IEntity } from '../../levels';
const { ccclass, property } = _decorator;



@ccclass('DoorManager')
export class DoorManager extends EntityManager {
  // 初始化
  async init(params:IEntity) {
    // 3. 初始化状态机
    this.fsm = this.addComponent(DoorStateMachine)
    await this.fsm.init()
    super.init(params)  // 调用父类初始化
    EventManager.Instance.on(EVENT_ENUM.DoorOpen,this.onOpen ,this)
  }

  onDestroy(){
    super.onDestroy()
    EventManager.Instance.off(EVENT_ENUM.DoorOpen,this.onOpen )
  }

  onOpen(){
    if(DataManager.Instance.enemies.every(enemy=>enemy.state===ENTITY_STATE_ENUM.DEATH)&&this.state!=ENTITY_STATE_ENUM.DEATH){
      this.state=ENTITY_STATE_ENUM.DEATH
    }
  }
}
